package main;

import gui.PurchaseInStoreGui;
import gui.AddRemoveTab;


import gui.*;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.SQLException;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import users.Clerk;
import users.Customer;
import users.Manager;

import database.dbConnect;


public class main extends JPanel {
	
	private Customer cust;
	
	//create instance of Clerk
	private Clerk clerk;
	private Manager manager;

    public main(Connection con) throws SQLException {
 
    	super(new GridLayout(1, 1));
    	
    	cust = new Customer(con);
    	
    	clerk = new Clerk(con);
    	
    	manager = new Manager(con);
    	
        // create tabbed pane
        JTabbedPane tabbedPane = new JTabbedPane();
        
        // panes related to customer
        JTabbedPane customerPanel = new JTabbedPane();
        
        tabbedPane.addTab("Customer", null, customerPanel,"Customer Transactions");
        
        // panels within customer tab
        customerPanel.addTab("New Account", null, new RegCustomer(cust),"Register New Customer Account");
        customerPanel.addTab("Close Account", null, new UnRegCustomer(cust), "Delete Customer Account");
        customerPanel.addTab("Order Online", null, new PurchaseOnline(cust), "Web Orders");
        
        
        //panes related to clerk
        JTabbedPane clerkPanel = new JTabbedPane();
        
        tabbedPane.addTab("Clerk", null, clerkPanel,"Clerk Transactions");
        
        // panels within clerk tab
        
        //add something        
        clerkPanel.addTab("Purchase of Item", null, new PurchaseInStoreGui(clerk), "In Store Purchase");
        
        clerkPanel.addTab("Return", null, new returnTabGui(clerk), "Process Return");
        
        JTabbedPane insertDelete = new JTabbedPane();
        tabbedPane.addTab("Insert and Delete", null, insertDelete, "Inserting Deleting Tuples");
        insertDelete.addTab("Add Item", null, new AddNewItem(manager), "Insert item");
        insertDelete.addTab("Remove Item", null, new RemoveItem(manager), "Delete item");
        insertDelete.addTab("AddRemoveStored", null, new AddRemoveStored(manager), "Insert or Delete Stored");
        insertDelete.addTab("AddRemoveShipment", null, new AddRemoveShipment(manager), "Insert or Delete Shipment");
        insertDelete.addTab("AddRemoveStore", null, new AddRemoveStore(manager), "Insert or Delete Store");
        insertDelete.addTab("AddRemoveSinger", null, new AddRemoveSinger(manager), "Insert or Delete Singer");
        insertDelete.addTab("AddRemoveShipItem", null, new AddRemoveShipItem(manager), "Insert or Delete ShipItem");
        insertDelete.addTab("RemoveReturn", null, new RemoveReturn(manager), "Delete Return");
        insertDelete.addTab("RemoveReturnItem", null, new RemoveReturnItem(manager), "Delete Return Item");
        insertDelete.addTab("RemovePurchase", null, new AddRemovePurchase(manager), "Remove Purchase");
        insertDelete.addTab("AddRemoveHasSong", null, new AddRemoveHasSong(manager), "Add or Remove HasSong");
        //panes related to manager
        JTabbedPane managerPanel = new JTabbedPane();
        
        tabbedPane.addTab("Manager", null, managerPanel,"Manager Transactions");
        
        // Panels within Manager tab
        managerPanel.addTab("Add or Remove a Supplier", null, new AddRemoveTab(manager), "Add/Remove Supplier");
        //managerPanel.addTab("Remove a Supplier", null, null, "Remove Supplier");
        managerPanel.addTab("Process Shipment", null, new ProcessShipment(manager), "Shipment");
        managerPanel.addTab("Process Customer Orders", null, new ProcessDeliveryOrder(manager), "Customer Orders");
        managerPanel.addTab("Daily Report", null, new DailySalesReport(manager), "Daily Sales Report");
 
        // Panes related to show rows of tables
        JTabbedPane tables = new JTabbedPane();
        
        tabbedPane.addTab("Show Table", null, tables, "Tables");
        
        // Panels within tables tab
        tables.addTab("Item", null, new Display(con, "Item"), "Show rows of item");
        tables.addTab("LeadSinger", null, new Display(con, "LeadSinger"), "Show rows of LeadSinger");
        tables.addTab("HasSong", null, new Display(con, "HasSong"), "Show rows of HasSong");
        tables.addTab("Supplier", null, new Display(con, "Supplier"), "Show rows of Supplier");
        tables.addTab("Shipment", null, new Display(con, "Shipment"), "Show rows of Shipment");
        tables.addTab("ShipItem", null, new Display(con, "ShipItem"), "Show rows of ShipItem");
        tables.addTab("Store", null, new Display(con, "Store"), "Show rows of Store");
        tables.addTab("Stored", null, new Display(con, "Stored"), "Show rows of Stored");
        tables.addTab("Purchase", null, new Display(con, "Purchase"), "Show rows of Purchase");
        tables.addTab("PurchaseItem", null, new Display(con, "PurchaseItem"), "Show rows of Purchase");
        tables.addTab("Customer", null, new Display(con, "Customer"), "Show rows of Customer");
        tables.addTab("Return", null, new Display(con, "Return"), "Show rows of Return");
        tables.addTab("ReturnItem", null, new Display(con, "ReturnItem"), "Show rows of ReturnItem");
       
        // Popular items panel
        JPanel popularPanel = new JPanel();
        
        tabbedPane.addTab("Popular Items", null, popularPanel, null);
        
        popularPanel.add(new PopularItems(cust));
        
        
        //Add the tabbed pane to this panel.
        add(tabbedPane);
        
        //The following line enables to use scrolling tabs.
        tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
    }

    
    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }
    
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from
     * the event dispatch thread.
     * @throws SQLException 
     */
    private static void createAndShowGUI() throws SQLException {
        //Create and set up the window.
    	Connection con = dbConnect.getConnection();
        JFrame frame = new JFrame("AMS MUSIC STORE");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        //Add content to the window.
        frame.add(new main(con), BorderLayout.CENTER);
        frame.setSize(1000, 700);
        
        frame.addWindowListener(new CloseAdapter(con));
        
        //Display the window.
        //frame.pack();
        frame.setVisible(true);
    }
    
    public static void main(String[] args) throws SQLException {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
					createAndShowGUI();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });
           
    }

    private static class CloseAdapter extends WindowAdapter
    {
    	private Connection con;
    	private CloseAdapter(Connection con) {
    		this.con = con;
    	}
    	@Override
    	public void windowClosed(WindowEvent arg0) {
    		dbConnect.closeConnection(con);
    	}
    }
}

